version 7
#delimit;

/* **************************************************************************** */
/* File:	nbrs-code.do							*/
/* Author:	Frederick J. Boehmke						*/
/* Date:	August 25, 2001							*/
/* Purpose:	STATA do-file to be called by some other do-file (or manually).	*/
/*										*/
/* This code constructs a variable (nbors) summing the value of the variable   	*/
/* _tmp for all contiguous states.  This can be used to generate the number	*/
/* of states that have adopted a policy or the total number of events 		*/
/* that have occured in neighboring states. If you want it lagged, do that	*/
/* after it is created.								*/
/*										*/
/* --IMPORTANT--								*/
/*										*/
/* Must be sorted by stateno (which is very specific - based on sort by full	*/
/* 	state name, NOT abbreviation). All 50 states MUST be included!		*/
/* Coding taking from Berry and Berry's 1990 AJPS article.			*/
/*										*/
/* Sample usage:								*/
/* gen _tmp=casino								*/
/* do nbrs-code.do								*/
/* rename nbors nbrscasn							*/
/*										*/
/* This creates a variable nbrscasn which adds the value of casino 		*/
/* for all neighboring states.							*/
/* **************************************************************************** */

sort year stateno;

drop nbors;
gen nbors= 0;

replace nbors =	_tmp[_n+8]+_tmp[_n+9]+_tmp[_n+23]+_tmp[_n+41]						if stateno==1;
replace nbors =	0											if stateno==2;
replace nbors =	_tmp[_n+03]+_tmp[_n+02]+_tmp[_n+28]+_tmp[_n+25]+_tmp[_n+41]				if stateno==3;
replace nbors =	_tmp[_n+14]+_tmp[_n+39]+_tmp[_n+32]+_tmp[_n+21]+_tmp[_n+13]+_tmp[_n+38]+_tmp[_n+20]	if stateno==4;
replace nbors =	_tmp[_n-02]+_tmp[_n+23]+_tmp[_n+32]							if stateno==5;
replace nbors =	_tmp[_n-03]+_tmp[_n+25]+_tmp[_n+38]+_tmp[_n+44]+_tmp[_n+10]+_tmp[_n+21]+_tmp[_n+30]	if stateno==6;
replace nbors =	_tmp[_n+14]+_tmp[_n+25]+_tmp[_n+32]							if stateno==7;
replace nbors =	_tmp[_n+12]+_tmp[_n+30]+_tmp[_n+22]							if stateno==8;
replace nbors =	_tmp[_n-08]+_tmp[_n+01]									if stateno==9;
replace nbors =	_tmp[_n-01]+_tmp[_n-09]+_tmp[_n+32]+_tmp[_n+30]+_tmp[_n+23]				if stateno==10;
replace nbors =	0											if stateno==11;
replace nbors =	_tmp[_n+35]+_tmp[_n+25]+_tmp[_n+16]+_tmp[_n+32]+_tmp[_n+38]+_tmp[_n+14]			if stateno==12;
replace nbors =	_tmp[_n+36]+_tmp[_n+02]+_tmp[_n+12]+_tmp[_n+04]+_tmp[_n+01]+_tmp[_n+09]			if stateno==13;
replace nbors =	_tmp[_n+03]+_tmp[_n-01]+_tmp[_n+08]+_tmp[_n+21]						if stateno==14;
replace nbors =	_tmp[_n+10]+_tmp[_n+12]+_tmp[_n+26]+_tmp[_n+08]+_tmp[_n+34]+_tmp[_n-02]			if stateno==15;
replace nbors =	_tmp[_n+20]+_tmp[_n-10]+_tmp[_n+11]+_tmp[_n+09]						if stateno==16;
replace nbors = _tmp[_n-13]+_tmp[_n+25]+_tmp[_n-04]+_tmp[_n-03]+_tmp[_n+08]+_tmp[_n+18]+_tmp[_n+29]+_tmp[_n+31]	if stateno==17;
replace nbors =	_tmp[_n+25]+_tmp[_n-14]+_tmp[_n+06]							if stateno==18;
replace nbors =	_tmp[_n+10]+_tmp[_n+02]									if stateno==19;
replace nbors =	_tmp[_n+26]+_tmp[_n+28]+_tmp[_n+18]+_tmp[_n-12]+_tmp[_n+10]				if stateno==20;
replace nbors =	_tmp[_n+18]+_tmp[_n-14]+_tmp[_n+11]+_tmp[_n+08]+_tmp[_n-02]+_tmp[_n+24]			if stateno==21;
replace nbors =	_tmp[_n+27]+_tmp[_n+01]+_tmp[_n-08]+_tmp[_n-13]						if stateno==22;
replace nbors =	_tmp[_n+11]+_tmp[_n+18]+_tmp[_n-08]+_tmp[_n+26]+_tmp[_n-01]				if stateno==23;
replace nbors =	_tmp[_n-06]+_tmp[_n-23]+_tmp[_n+18]+_tmp[_n-20]						if stateno==24;
replace nbors = _tmp[_n-21]+_tmp[_n+11]+_tmp[_n-09]+_tmp[_n+02]+_tmp[_n-10]+_tmp[_n-12]+_tmp[_n-08]+_tmp[_n+17]	if stateno==25;
replace nbors =	_tmp[_n-14]+_tmp[_n+24]+_tmp[_n+15]+_tmp[_n+08]						if stateno==26;
replace nbors =	_tmp[_n-11]+_tmp[_n-21]+_tmp[_n+23]+_tmp[_n+14]+_tmp[_n-12]+_tmp[_n-02]			if stateno==27;
replace nbors =	_tmp[_n-23]+_tmp[_n+09]+_tmp[_n-16]+_tmp[_n+16]+_tmp[_n-25]				if stateno==28;
replace nbors =	_tmp[_n-08]+_tmp[_n+16]+_tmp[_n-10]							if stateno==29;
replace nbors =	_tmp[_n-22]+_tmp[_n+08]+_tmp[_n+02]+_tmp[_n-10]						if stateno==30;
replace nbors =	_tmp[_n-28]+_tmp[_n+13]+_tmp[_n-25]+_tmp[_n+05]+_tmp[_n+12]				if stateno==31;
replace nbors =	_tmp[_n+06]+_tmp[_n-02]+_tmp[_n-25]+_tmp[_n-11]+_tmp[_n+13]				if stateno==32;
replace nbors =	_tmp[_n+07]+_tmp[_n-23]+_tmp[_n+09]+_tmp[_n+13]						if stateno==33;
replace nbors =	_tmp[_n+07]+_tmp[_n-08]+_tmp[_n-11]							if stateno==34;
replace nbors =	_tmp[_n-18]+_tmp[_n-21]+_tmp[_n-13]+_tmp[_n+03]+_tmp[_n+13]				if stateno==35;
replace nbors =	_tmp[_n+07]+_tmp[_n-05]+_tmp[_n-30]+_tmp[_n-20]+_tmp[_n-11]+_tmp[_n-32]			if stateno==36;
replace nbors =	_tmp[_n-32]+_tmp[_n-09]+_tmp[_n-25]+_tmp[_n+10]						if stateno==37;
replace nbors =	_tmp[_n-30]+_tmp[_n-18]+_tmp[_n+10]+_tmp[_n-03]+_tmp[_n-06]+_tmp[_n-08]			if stateno==38;
replace nbors =	_tmp[_n-32]+_tmp[_n-18]									if stateno==39;
replace nbors =	_tmp[_n-30]+_tmp[_n-07]									if stateno==40;
replace nbors =	_tmp[_n-07]+_tmp[_n-14]+_tmp[_n+09]+_tmp[_n-15]+_tmp[_n-18]+_tmp[_n-26]			if stateno==41;
replace nbors =	_tmp[_n-09]+_tmp[_n-32]+_tmp[_n-41]+_tmp[_n-18]+_tmp[_n-38]+_tmp[_n-17]+_tmp[_n-25]+_tmp[_n+04]	if stateno==42;
replace nbors =	_tmp[_n-12]+_tmp[_n-07]+_tmp[_n-39]+_tmp[_n-25]						if stateno==43;
replace nbors =	_tmp[_n-41]+_tmp[_n-16]+_tmp[_n-32]+_tmp[_n+06]+_tmp[_n-38]+_tmp[_n-13]			if stateno==44;
replace nbors =	_tmp[_n-16]+_tmp[_n-24]+_tmp[_n-13]							if stateno==45;
replace nbors =	_tmp[_n-13]+_tmp[_n-04]+_tmp[_n-29]+_tmp[_n+02]+_tmp[_n-26]				if stateno==46;
replace nbors =	_tmp[_n-10]+_tmp[_n-35]									if stateno==47;
replace nbors =	_tmp[_n-02]+_tmp[_n-31]+_tmp[_n-13]+_tmp[_n-10]+_tmp[_n-28]				if stateno==48;
replace nbors =	_tmp[_n-26]+_tmp[_n-34]+_tmp[_n-36]+_tmp[_n-27]						if stateno==49;
replace nbors =	_tmp[_n-44]+_tmp[_n-06]+_tmp[_n-38]+_tmp[_n-24]+_tmp[_n-09]+_tmp[_n-23]			if stateno==50;

drop _tmp;